<template>
   
  <div>
       
    <el-container>
           
      <el-header>
               
        <router-link to="/blogs">
                 
          <img
            src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
            style="height: 60%; margin-top: 10px"
          />
                 
        </router-link>
             
      </el-header>
           
      <el-main>
               
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
                   
          <el-form-item label="用户名" prop="username">
                       
            <el-input
              type="text"
              maxlength="12"
              v-model="ruleForm.username"
            ></el-input>
                     
          </el-form-item>
                   
          <el-form-item label="密码" prop="password">
                       
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
            ></el-input>
                     
          </el-form-item>
                   
          <el-form-item>
                       
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
                       
            <el-button @click="resetForm('ruleForm')">重置</el-button>          
          </el-form-item>
                 
        </el-form>
             
      </el-main>
         
    </el-container>
     
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "111111",
        username: "studydog",
      },
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async submitForm(formName) {
      try {
        this.$refs[formName].validate(async (valid) => {
          // 对ref为ruleForm的form表单进行rules的规则验证，返回验证结果valid=>true/false
          if (valid) {
            // 提交逻辑
            const res = await this.$axios.post("/login", this.ruleForm);
            const jwt = res.headers["authorization"]; //头文件判断
            const userInfo = res.data.data;
            console.log(userInfo);
            //const token = res.headers["authorization"];
            this.$store.commit("SET_TOKEN", jwt);
            this.$store.commit("SET_USERINFO", userInfo);
            console.log('session',sessionStorage.getItem("userInfo"))
            // vuex保存信息
            
            this.go("/home"); // 路由跳转(main.js全局混入)
          } else {
            console.log("error submit!!"); // 表单验证错误
            return false;
          }
        });
      } catch (e) {
        console.log(e.message || e); // 错误内容
        console.log("api error!!"); // try{}内代码执行错误
      }
    },
    // submitForm(formName) {
    //   const _this = this;
    //   this.$refs[formName].validate((valid) => {
    //     // 对ref为ruleForm的form表单进行rules的规则验证，返回验证结果valid=>true/false
    //     if (valid) {

    //       // 提交逻辑
    //       this.$axios
    //         .post("/login", this.ruleForm)
    //         // api接口'/login', 请求参数this.ruleForm对象
    //         .then((res) => {
    //             // console.log(res.headers)
    //             // console.log(res)
    //             const jwt=res.headers["authorization"]//头文件判断
    //             const userInfo=res.data.data
    //             console.log(userInfo)
    //           //const token = res.headers["authorization"];
    //           _this.$store.commit("SET_TOKEN", jwt);
    //           _this.$store.commit("SET_USERINFO", userInfo);
    //           // vuex保存信息
    //           _this.$router.push("/blogs");// 路由跳转
    //         });
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    // },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    this.$notify({
      title: "请进行登录",
      message: "请进行登录",
      duration: 1500,
    });
  },
};
</script>


<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-main {
  /* background-color: #e9eef3; */
  color: #333;
  text-align: center;
  line-height: 160px;
}
.mlogo {
  height: 80%;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>
